unit GMedParkPNr;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, GFixForm, bendri, Sandelys_dm, StdCtrls, Mask,
  DBCtrls, Buttons, ActnList,
  ExtCtrls, cxLookAndFeelPainters, cxButtons, cxTextEdit,
  cxDBEdit, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox, cxControls, cxContainer, cxEdit, cxMaskEdit, cxCalc,
  DB, cxPropertiesStore, Menus, cxGraphics, cxLabel;

type
  TfMedParkPNr = class(TGamaFixForm)
    sMode: TStaticText;
    ActionList1: TActionList;
    aPaimtiPreke: TAction;
    pOK: TPanel;
    bCancel: TcxButton;
    bOK: TcxButton;
    DS: TDataSource;
    Label1: TLabel;
    GamaPropertiesStore: TcxPropertiesStore;
    Label3: TLabel;
    eStoris: TcxDBLookupComboBox;
    Label6: TLabel;
    ePlotis: TcxDBLookupComboBox;
    Label9: TLabel;
    eRusis: TcxDBLookupComboBox;
    ePNumeris: TcxTextEdit;
    cxLabel1: TcxLabel;
    ePText: TcxMaskEdit;
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure eSNChange(Sender: TObject);
  private
    { Private declarations }
    PVM: double;
    procedure SetMode(v: string);
  public
    { Public declarations }
    procedure ClearMode;
    property Mode: string write SetMode;
  end;

var
  fMedParkPNr: TfMedParkPNr;

implementation

uses
  DBClient, Math, sandelys_conf, doktip,  uSandDokTip, med_dm,
  tools, JvJCLUtils;

{$R *.dfm}

procedure TfMedParkPNr.FormShow(Sender: TObject);
begin
  DS.Enabled := true;

  ActiveControl := ePText;
  //
  ePText.Properties.OnEditValueChanged := eSNChange;

  eSNChange(nil);
end;

procedure TfMedParkPNr.eSNChange(Sender: TObject);
var
  tbSQL: TClientDataSet;
  strPNr: String;
  newPNr: String;
  nrStrPNr: String;
  nrPNr: Integer;
begin
  strPNr := VarToStr(ePText.EditValue);

  if strPNr = '' then Exit;

  tbSQL := dmBendras.GetTempSQL;
  tbSQL.CommandText :=
   'SELECT max(substring(P.PNr FROM'
    +' CHAR_LENGTH('''+strPNr+''')+2 FOR CHAR_LENGTH(P.PNr))) FROM medPNr P '
    +' WHERE (substring(P.PNr FROM 1 FOR CHAR_LENGTH('''+strPNr+''')) = '''+strPNr+''')'
  ;

  tbSQL.Open;
  if VarIsNull(tbSQL.Fields[0].AsVariant) then begin
    nrPNr := 1;
    nrStrPNr := FormatFloat('00000', nrPNr );
    newPNr := trim(strPNr) + '-' + nrStrPNr;
  end
  else begin
    nrStrPNr := tbSQL.Fields[0].AsString;
    nrPNr := StrToInt(nrStrPNr)+1;
    nrStrPNr := FormatFloat('00000', nrPNr );
    newPNr := trim(strPNr) + '-' + nrStrPNr;
  end;
  ePNumeris.Text := nrStrPNr;
  DS.DataSet['Pnr'] := newPNr;

end;

procedure TfMedParkPNr.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  ePText.Properties.OnEditValueChanged := nil;

  DS.Enabled := false;
end;

procedure TfMedParkPNr.SetMode(v: string);
begin
  sMode.Caption := v;
end;

procedure TfMedParkPNr.ClearMode;
begin
  sMode.Caption := '';
end;

procedure TfMedParkPNr.FormCreate(Sender: TObject);
begin
  DS.Enabled := false;
end;

end.
